﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml.Data;

namespace G.Data
{
    /// <summary>
    /// This class is responsible for retrieving data.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class IncrementalLoader<T> : IAsyncOperation<LoadMoreItemsResult>
    {
        private AsyncStatus _asyncStatus = AsyncStatus.Started;
        private LoadMoreItemsResult _results;

        public IncrementalLoader(IncrementalLoadingCollection<T> incrementalLoadingCollection, uint count)
        {
            PullDataIncrementalData(incrementalLoadingCollection, count);
        }

        public async void PullDataIncrementalData(IncrementalLoadingCollection<T> incrementalLoadingCollection, uint count)
        {
            try
            {
                incrementalLoadingCollection.IsLoadingData = true;
                IncrementalLoadingResult<T> result = await incrementalLoadingCollection.PullDataAsync(++incrementalLoadingCollection.PageNum, incrementalLoadingCollection.PageSize);
                if (result != null)
                {
                    if (result.Items != null && result.Items.Count() > 0)
                    {
                        incrementalLoadingCollection.AddRange(result.Items);
                        incrementalLoadingCollection.TotalItemsCount = result.TotalItemsCount;
                    }
                    else
                    {
                        incrementalLoadingCollection.HasMoreItems = false;
                    }
                }
                else
                {
                    throw new InvalidDataException();
                }

                _asyncStatus = AsyncStatus.Completed;
            }
            catch (Exception ex)
            {
                this.errorCode = ex;
                _results.Count = 0;
                _asyncStatus = AsyncStatus.Error;
                incrementalLoadingCollection.HasMoreItems = false;
            }

            incrementalLoadingCollection.IsLoadingData = false;

            if (Completed != null)
            {
                Completed(this, _asyncStatus);
            }
        }

        public AsyncOperationCompletedHandler<LoadMoreItemsResult> Completed { get; set; }

        public LoadMoreItemsResult GetResults()
        {
            return _results;
        }

        public void Cancel()
        {
            throw new NotImplementedException();
        }

        public void Close()
        {
        }

        private Exception errorCode;
        public Exception ErrorCode
        {
            get { return errorCode; }
        }

        public uint Id
        {
            get { throw new NotImplementedException(); }
        }

        public AsyncStatus Status
        {
            get { return _asyncStatus; }
        }
    }
}